<?php
/*
 * Copyright 2008 by Oliver Radwan, Maxwell Palmer, Nolan McNair,
 * Taylor Talmage, and Allen Tucker.  This program is part of RMH Homebase.
 * RMH Homebase is free software.  It comes with absolutely no warranty.
 * You can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
*/

/*
 * class Shift characterizes a time interval in a day
 * for scheduling volunteers
 * @version May 1, 2008, modified 9/15/08, 2/14/10
 * @author Allen Tucker and Maxwell Palmer
*/
 class Shift {
	private $mm_dd_yy;      // String: "mm-dd-yy".
 	private $name;          // String: '9-12', '12-3', '3-6', '9-12Fam', '12-3Fam', '3-6Fam',
 							// '6-9', '10-1', '1-4', '12-2', '2-5', 'night', or 'chef'
    private $vacancies;         // number of vacancies in this shift
    private $persons;       // array of person ids filling slots, followed by their name, ie "malcom1234567890+Malcom+Jones"
    private $sub_call_list; // "yes" or "no" for if SCL exists
    private $day;	// string name of day "Monday"...
    private $id;            // "mm-dd-yy-ss-ss" or "mm-dd-yy-ss-ssFam" or "mm-dd-yy-chef" is a unique key for this shift
    						// the suffix "Fam" designates a shift on the family room calendar
    						// the suffix "Chef" designates a guest chef on a particular date
	 private $notes;		// notes written by the manager
    /*
     * construct an empty shift with a certain number of vacancies
     */
    function __construct ($id, $vacancies,$persons,$sub_call_list,$notes) {
    	$this->mm_dd_yy=substr($id,0,8);
		$this->vacancies=$vacancies;
    	$this->id = $id;
    	$this->name=substr($id, 9);
    	$this->day = date("D", mktime(0, 0, 0, substr($this->mm_dd_yy,0,2),
			substr($this->mm_dd_yy,3,2), "20".substr($this->mm_dd_yy,6,2)));
		if ($persons==null) {
		   $this->persons=array();
		   $this->notes="";
		   $this->sub_call_list = "no";
		}
		else {
		   $this->persons = $persons;
    	   $this->sub_call_list = $sub_call_list;
		   $this->notes=$notes;
		}
    }

    /*
     * @return the number of vacancies in this shift.
     */
    function num_vacancies() {
    	return $this->vacancies;
    }
    function ignore_vacancy() {
    	if ($this->vacancies>0)
    		--$this->vacancies;
    }
    function add_vacancy() {
    	++$this->vacancies;
    }
	 function num_slots() {
		if(!$this->persons[0])
			array_shift($this->persons);
		return $this->vacancies + count($this->persons);
	}
    function has_sub_call_list () {
    	if($this->sub_call_list=="yes")
    		return true;
    	return false;
    }
    function open_sub_call_list() {
    	$this->sub_call_list="yes";
    }
    function close_sub_call_list() {
    	$this->sub_call_list="no";
    }
    /*
     * getters and setters
     */
    function get_name() {
    	return $this->name;
    }
    /*
   * Creates the $shift_name of a shift, e.g. "Sunday, February 14, 2010 from 2pm to 5pm"
   *         from its $id, e.g. "02-14-10-2-5"
   */
  function get_shift_name_from_id($id) {
 	$shift_name=date("l, F jS, Y",mktime(0,0,0,substr($id,0,2),substr($id,3,2),substr($id,6,2)));
 	$name=substr($id,9);
 	$idnames = array("9-12"=>" from 9am to 12pm", "12-3"=>" from 12pm to 3pm",
 					"3-6"=>" from 3pm to 6pm", "6-9"=>" from 6pm to 9pm",
 					"10-1"=>" from 10am to 1pm", "1-4"=>" from 1pm to 4pm",
 					"2-5"=>" from 2pm to 5pm", "5-9"=>" from 5pm to 9pm",
 					'night'=>" - night shift",
 					"chef"=>" - guest chef",
 					"9-12Fam"=>" Family Room from 10am to 1pm",
					"12-3Fam"=>" Family Room from 1pm to 4pm",
 					"3-6Fam"=>" Family Room from 4pm to 7pm",
 					);
	$shift_name = $shift_name . $idnames[$name];
	return $shift_name;
 }

    function get_persons() {
    	return $this->persons;
    }
    function get_sub_call_list() {
    	return $this->sub_call_list;
    }
    function get_id() {
    	return $this->id;
    }
    function get_day() {
    	return $this->day;
    }
	 function get_notes() {
		return $this->notes;
	}
	function set_notes($notes) {
		$this->notes=$notes;
	}
    function assign_persons($p) {
    	$this->persons=$p;
    }
 }
?>
